/*
Copyright (c) <2010>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package richmediaplus.utils
{
	import flash.display.Sprite;
	
	import mx.controls.Alert;
	import mx.utils.ObjectUtil;
	/**
	 * AlertUtil
	 * Alert Manager in Flex Porject
	 * @author Alvin / Aedis.Ju
	 * @blog http://blog.richmediaplus.com
	 * @code http://code.google.com/p/alertutil/
	 */
	public class AlertUtil
	{
		//--------------------------------------------------------------------------
		//  Private variables
		//--------------------------------------------------------------------------
		[Embed(source="assets/warning.png")]
		[Bindable]
		private static var warningIcon:Class;
		[Embed(source="assets/question.png")]
		[Bindable]
		private static var questionIcon:Class;
		[Embed(source="assets/info.png")]
		[Bindable]
		private static var infoIcon:Class;
		[Embed(source="assets/error.png")]
		[Bindable]
		private static var errorIcon:Class;

		private static var msgXML:XML;

		//--------------------------------------------------------------------------
		//  Public static methods
		//--------------------------------------------------------------------------
		/**
		 * Set the Message XML Data to use AlertUtil.
		 * @param value Message Data
		 */
		public static function setMsgXML(value:XML):void
		{
			msgXML = value;
		}

		/**
		 * Show OK Message.
		 * @param msgNode Specifies the message xmlNode for find text message in message XML.
		 * @param closeHandler Specifies the listener for click events from the buttons.
		 * @param parameters Message's parameters.
		 * @param parent The parent object of the Alert control.
		 * @param title Specifies the dialog box title.
		 * @return mx.controls.Alert
		 */		
		public static function showOKMsg(msgNode:String = "", closeHandler:Function = null, parameters:Array = null, parent:Sprite = null, title:String = ""):Alert
		{
			return showMsg(msgNode, title, Alert.OK, parent, closeHandler, Alert.OK, parameters);
		}

		/**
		 * Show YES|NO Message
		 * @param msgNode Specifies the message xmlNode for find text message in message XML.
		 * @param closeHandler Specifies the listener for click events from the buttons.
		 * @param parameters Message's parameters.
		 * @param parent The parent object of the Alert control.
		 * @param defaultButtonFlag A bitflag that specifies the default button.
		 * @param title Specifies the dialog box title.
		 * @return mx.controls.Alert
		 */		
		public static function showYesNoMsg(msgNode:String = "", closeHandler:Function = null, parameters:Array = null, parent:Sprite = null, defaultButtonFlag:uint = 0x0002, title:String = ""):Alert
		{
			return showMsg(msgNode, title, Alert.YES | Alert.NO, parent, closeHandler, defaultButtonFlag, parameters);
		}
		
		/**
		 * Show YES|CANCEL Message
		 * @param msgNode Specifies the message xmlNode for find text message in message XML.
		 * @param closeHandler Specifies the listener for click events from the buttons.
		 * @param parameters Message's parameters.
		 * @param parent The parent object of the Alert control.
		 * @param defaultButtonFlag A bitflag that specifies the default button.
		 * @param title Specifies the dialog box title.
		 * @return mx.controls.Alert
		 */		
		public static function showYesCancelMsg(msgNode:String, closeHandler:Function = null, parameters:Array = null, parent:Sprite = null, defaultButtonFlag:uint = 0x0008, title:String = ""):Alert
		{
			return showMsg(msgNode, title, Alert.YES | Alert.CANCEL, parent, closeHandler, defaultButtonFlag, parameters);
		}

		/**
		 * Show YES|NO|CANCEL Message.
		 * @param msgNode Specifies the message xmlNode for find text message in message XML.
		 * @param closeHandler Specifies the listener for click events from the buttons.
		 * @param parameters Message's parameters.
		 * @param parent The parent object of the Alert control.
		 * @param defaultButtonFlag A bitflag that specifies the default button.
		 * @param title Specifies the dialog box title.
		 * @return mx.controls.Alert
		 * 
		 */		
		public static function showYesNoCancelMsg(msgNode:String, closeHandler:Function = null, parameters:Array = null, parent:Sprite = null, defaultButtonFlag:uint = 0x0008, title:String = ""):Alert
		{
			return showMsg(msgNode, title, Alert.YES | Alert.NO | Alert.CANCEL, parent, closeHandler, defaultButtonFlag, parameters);
		}
		
		//--------------------------------------------------------------------------
		//  Private static methods
		//--------------------------------------------------------------------------
		/**
		 * Set AlertUtil's rules and show mx.controls.Alert.
		 * @param msgNode Specifies the message xmlNode for find text message in message XML.
		 * @param title Specifies the dialog box title.
		 * @param flags Message Type.
		 * @param parent The parent object of the Alert control.
		 * @param closeHandler Specifies the listener for click events from the buttons.
		 * @param defaultButtonFlag A bitflag that specifies the default button.
		 * @param parameters Message's parameters.
		 * @return mx.controls.Alert
		 */		
		private static function showMsg(msgNode:String, title:String = "", flags:uint = 0x4, parent:Sprite = null, closeHandler:Function = null, defaultButtonFlag:uint = 0x4, parameters:Array = null):Alert
		{
			//Check the messageData
			if (!msgXML)
				return Alert.show("Please Set the Message Data First!", "richmediaplus.utils.AlertUtil");
			//Get message type
			var msgArr:Array = msgNode.split(".");
			var msgArrLen:int = msgArr.length;
			if (msgArrLen <= 1)
			{
				return Alert.show("Please Check Message Parameter!", "richmediaplus.utils.AlertUtil");
			}
			//Set message type & message icon
			var msgType:String = msgArr[0];
			var msgIcon:Class;
			if (msgType == "info")
			{
				msgIcon = infoIcon;
			}
			else if (msgType == "warning")
			{
				msgIcon = warningIcon;
			}
			else if (msgType == "question")
			{
				msgIcon = questionIcon;
			}
			else if (msgType == "error")
			{
				msgIcon = errorIcon;
			}
			else
			{
				return Alert.show("Undefined Message Type!", "richmediaplus.utils.AlertUtil");
			}
			//Get message title
			var msgTitle:String;
			if (title != "")
			{
				msgTitle = title;
			}
			else
			{
				msgTitle = msgXML.title;
			}
			//Get message text
			var tmpMsgXML:Object = ObjectUtil.copy(msgXML);
			for (var i:int = 0; i < msgArrLen; i++)
			{
				tmpMsgXML = tmpMsgXML[msgArr[i]];
			}
			var msgText:String = tmpMsgXML.toString();
			if (msgText == "")
			{
				return Alert.show("Please Check Message Text!", "richmediaplus.utils.AlertUtil");
			}
			//Set message parameters
			if (parameters)
			{
				for (var p:int = 0; p < parameters.length; p++)
				{
					msgText = msgText.replace("[" + p + "]", parameters[p]);
				}
			}
			//Set your Alert style here
			Alert.buttonWidth = 70;
			
			//Return Alert
			return Alert.show(msgText, msgTitle, flags, parent, closeHandler, msgIcon, defaultButtonFlag);
		}
	}
}